<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 8/9/17
 * Time: 3:34 PM
 */
namespace AdminBundle\Controller;

class FileController extends AdminController
{
    /*
     * 文件分类
     */
    public function fileCategoryAction()
    {

        if(!$this->isPageLogined()){
            return $this->render('AdminBundle:Auth:login.html.twig');
        }

        $conn = $this->get("database_connection");


        // 获取文件总数
        $all_sql = "SELECT count(*) FROM file WHERE file_category_id IN (SELECT id FROM file_category WHERE";


        if($this->this_admin_role_id == 4){
            $all_sql .= " admin_id = '". $this->this_admin_id ."' ) ";
        } else {
            $all_sql .= " district_id = '". $this->this_district_id ."' ) ";
        }


        $data['all_count'] = $conn->fetchColumn($all_sql);

        // 本月文件总数
        $now_month = date('Y-m');
        $month_sql = $all_sql . " AND DATE_FORMAT(created_at,'%Y-%m')  = '". $now_month ."'";
        $data['month_count'] = $conn->fetchColumn($month_sql);


        // 今日文件总数
        $now_day = date('Y-m-d');
        $day_sql = $all_sql . " AND DATE_FORMAT(created_at,'%Y-%m-%d')  = '". $now_day ."'";
        $data['day_count'] = $conn->fetchColumn($day_sql);


        return $this->render("AdminBundle:File:categoryList.html.twig",array("data"=>$data));
    }

    /*
     * 文件列表
     */
    public function fileListAction($id)
    {
        if(!$this->isPageLogined()){
            return $this->render('AdminBundle:Auth:login.html.twig');
        }
        return $this->render("AdminBundle:File:fileList.html.twig",array("id"=>$id));
    }

    /*
     * 发布/编辑文件
     */
    public function editFileAction($id = 0)
    {
        if(!$this->isPageLogined()){
            return $this->render('AdminBundle:Auth:login.html.twig');
        }

        $data = $this->getApiData("ApiBundle:Admin/File:getFileDetail",array("id" => $id));

        if(empty($id)){
            $data['data']['info']['id'] = "0";
        }

        $conn = $this->get("database_connection");
        $data["category"] = $conn->fetchAll("SELECT * FROM company_category WHERE 1 ");
        $data["attribute"] = $conn->fetchAll("SELECT * FROM company_attribute WHERE 1 ");
        $data['teac_admin'] = $conn->fetchAll("SELECT id,nickname FROM admin WHERE admin_role_id = 4 AND district_id = '". $this->get("session")->get("district_id") ."'");

        return $this->render("AdminBundle:File:editFile.html.twig",array("data" => $data));
    }
}